package com.menu;

import com.infomanage.enums.RoleType;
import com.infomanage.po.User;

public abstract class MenuUtil {
	
	public static Menu menu = null;
	
	/**
	 * 构建主菜单
	 * @return
	 */
	public static String buildMainMenu(User user) {
		initMenuItems(user);
		String html = menu.doView();
		return html;
	}
	
	public static void initMenuItems(User user) {
		menu = new Menu();
		// 信息管理
		MenuItem item = new MenuItem("日常管理", "");
		item.setSelected(true);
		menu.addItem(item);
		initMenuItem1(item);
		
		
		// 救援决策支持
		item = new MenuItem("决策支持", "");
		item.setPosition(1);
		menu.addItem(item);
		initMenuItem2(item);
		
		
		// 矿井环境实时监测系统
		item = new MenuItem("远程环境监测", "");
		menu.addItem(item);
		item.setPosition(1);
		initMenuItem3(item);
		
		// 实训演练
		item = new MenuItem("实训演练", ""); 
		menu.addItem(item);
		item.setPosition(1);
		initMenuItem4(item);
		
		// 应急通信
		item = new MenuItem("应急通信", "");
		item.setPosition(1);
		menu.addItem(item);
		initMenuItem6(item);
		
		// 救援管理
		item = new MenuItem("救援管理", "");
		menu.addItem(item);
		initMenuItem7(item);
		
//		// 质量标准化
		item = new MenuItem("质量标准化", "");
		item.setType(1);// 树形菜单
		menu.addItem(item);
		
		// 视频监控
		item = new MenuItem("视频监控", "");
		menu.addItem(item);
		item.setPosition(1);
		initMenuItem9(item);
		
		// 车辆监控
		item = new MenuItem("车辆监控", "");
		menu.addItem(item);
		item.setPosition(1);
		initMenuItem10(item);
		

		
		// 系统维护
		if(user != null && user.getRoleId() == RoleType.SYSADMIN.value()){
			item = new MenuItem("系统维护", "");
			item.setPosition(1);// 放在左侧
			menu.addItem(item);
			initMenuItem5(item);
		}
	}
	
	/**
	 * 日常管理 quality standardization
	 */
	public static MenuItem initMenuItem1(MenuItem item) {
		String target = "";
		MenuItem subItem = new MenuItem("工作日志", "");
		subItem.setSelected(true);
		item.addChild(subItem);
		subItem.addChild("人员考勤", "infomanage/worklog/attendance_list.jsp", target)
			   .addChild("当班日志", "infomanage/worklog/atworklog_list.jsp", target)
			   .addChild("当班人员", "infomanage/worklog/dutyPerson_list.jsp", target)
			   .addChild("考勤统计", "infomanage/worklog/statis_list.jsp", target);
		
		subItem = new MenuItem("计划总结", "");
		item.addChild(subItem);
		subItem.addChild("职务调整", "infomanage/plan/positionAdjust_list.jsp", target)
			   .addChild("干部储备", "infomanage/plan/leaderReserve_list.jsp", target)
			   .addChild("教育与培训", "infomanage/plan/educationTrain_list.jsp", target)
			   .addChild("装备计划", "infomanage/plan/equipmentPlan_list.jsp", target)
			   .addChild("矿井预防检查", "infomanage/plan/preventCheck_list.jsp", target)
			   .addChild("内务管理", "infomanage/plan/affairManage_list.jsp", target)
			   .addChild("设备维修", "infomanage/plan/deviceRepair_list.jsp", target);
		
		
		subItem = new MenuItem("学习考核", "");
		item.addChild(subItem);
		subItem.addChild("理论学习", "infomanage/study/theory_list.jsp", target)
			   .addChild("日常训练", "infomanage/study/militaryTrain_list.jsp", target)
			   .addChild("急救训练", "infomanage/study/rescueTrain_list.jsp", target)
			   .addChild("技术业务训练", "infomanage/study/techBiz_list.jsp", target)
			   .addChild("体质训练", "infomanage/study/physiqueTrain_list.jsp", target)
			   .addChild("体质测试", "infomanage/study/physiqueTest_list.jsp", target)
			   .addChild("心理测试", "infomanage/study/psychometry_list.jsp", target)
			   .addChild("佩机演习", "infomanage/study/manoeuvre_list.jsp", target);
		
		subItem = new MenuItem("装备管理", "");
		item.addChild(subItem);
		subItem.addChild("装备管理", "infomanage/equmgmt/equmgmt_list.jsp", target)
			   .addChild("使用统计", "infomanage/equmgmt/useStatis_list.jsp", target)
			   .addChild("报废记录", "infomanage/equmgmt/discard_list.jsp", target);
		
		subItem = new MenuItem("技术管理", "");
		item.addChild(subItem);
		subItem.addChild("维护日志", "infomanage/techmgmt/ambulance_list.jsp", target)
			   .addChild("交接班记录", "infomanage/techmgmt/exchange_list.jsp", target)
			   .addChild("考核记录", "infomanage/techmgmt/check_list.jsp", target)
			   .addChild("奖惩记录", "infomanage/techmgmt/reward_list.jsp", target)
			   .addChild("事故处理", "infomanage/techmgmt/accident_list.jsp", target)
			   .addChild("材料消耗", "infomanage/techmgmt/material_list.jsp", target)
			   .addChild("技术安全", "infomanage/techmgmt/security_list.jsp", target);
		
		subItem = new MenuItem("应急管理", "");
		item.addChild(subItem);
		subItem.addChild("交通路线", "infomanage/emergency/trafficRoutePic_list.jsp", target)
			   .addChild("灾害预防", "infomanage/emergency/disasterPrevent_list.jsp", target)
			   .addChild("通风系统", "infomanage/emergency/flowwindSys_list.jsp", target)
			   .addChild("事故总结", "infomanage/emergency/accidentSummary_list.jsp", target)
			   .addChild("联系方式", "infomanage/emergency/contactInfo_list.jsp", target);
		
		subItem = new MenuItem("规则制度", "");
		item.addChild(subItem);
		subItem.addChild("规章制度", "infomanage/rule/rule_list.jsp", target)
			   .addChild("会议纪要", "infomanage/rule/meeting_list.jsp", target);
		
		subItem = new MenuItem("人员管理", "");
		item.addChild(subItem);
		subItem.addChild("人员管理", "infomanage/hr/personManage_list.jsp", target);
		
		return item;
	}
	
	
	
	// 救援决策支持
	public static MenuItem initMenuItem2(MenuItem item) {
		String target = "";
		
		MenuItem subItem = new MenuItem("专家咨询", "dss/ecs/ecs_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		subItem = new MenuItem("专家管理", "dss/ecs/expert_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		/*
		subItem.addChild("专家咨询", "dss/ecs/ecs_list.jsp", target)
			   .addChild("专家管理", "dss/ecs/expert_list.jsp", target);
		*/
		
		subItem = new MenuItem("救援技术", "dss/rt/rt_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		subItem = new MenuItem("事故案例管理", "dss/case/case_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		subItem = new MenuItem("远程调度预案查询", "dss/preplan/preplan_map.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		subItem = new MenuItem("远程调度预案管理", "dss/preplan/preplan_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		subItem = new MenuItem("救援路线", "dss/re/re_map.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		return item;
	}
	
	
	// 矿井环境实时监测系统
	public static MenuItem initMenuItem3(MenuItem item) {
		MenuItem subItem = new MenuItem("实时曲线", "envconsole/chart_wave.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
//		subItem = new MenuItem("实时数据", "envconsole/sync_table.jsp");
//		subItem.setPosition(1);
//		item.addChild(subItem);
		subItem = new MenuItem("统计分析", "envconsole/chart_stat.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
//		subItem = new MenuItem("波动分析", "envconsole/chart_contrast.jsp");
//		subItem.setPosition(1);
//		item.addChild(subItem);
		subItem = new MenuItem("对比分析", "envconsole/chart_contrast_bar.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		subItem = new MenuItem("预警分析", "envconsole/chart_warn.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		return item;
	}
	
	
	// 应急通信
	public static MenuItem initMenuItem6(MenuItem item) {
		String target = "";
		/*
		MenuItem subItem = new MenuItem("发送短信", "");
		item.addChild(subItem);
		subItem.addChild("发件箱", "sms/sendsms/smsbox.jsp", target);
		*/
		MenuItem subItem = new MenuItem("短信通知", "sms/sendsms/smsbox.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		subItem = new MenuItem("内部通知", "infomanage/msg/messageManage_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		return item;
	}
	
	// 实训演练
	public static MenuItem initMenuItem4(MenuItem item) {
		MenuItem subItem = new MenuItem("业务培训", "train/train_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		subItem = new MenuItem("理论培训", "train/train_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		subItem = new MenuItem("虚拟现实培训", "train/train_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		subItem = new MenuItem("考核管理", "train/train_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		return item;
	}
	
	// 应急救援
	public static MenuItem initMenuItem7(MenuItem item) {
		String target = "";
		MenuItem subItem = new MenuItem("事故处理", "");
		item.addChild(subItem);
		subItem.addChild("电话记录", "rescue/dealaccident/phonelog.jsp", target)
			   .addChild("位置信息", "rescue/position/location_map.jsp", target)
			   .addChild("短信通知", "rescue/dealaccident/smsbox.jsp", target);
		
		subItem = new MenuItem("预案管理", "");
		item.addChild(subItem);
		subItem.addChild("交通路线", "rescue/planmanage/trafficRoutePic_list.jsp", target)
			   .addChild("灾害预防", "rescue/planmanage/disasterPrevent_list.jsp", target)
			   .addChild("通风系统", "rescue/planmanage/flowwindSys_list.jsp", target)
			   .addChild("事故总结", "rescue/planmanage/accidentSummary_list.jsp", target)
			   .addChild("联系方式", "rescue/planmanage/contactInfo_list.jsp", target);
		
		subItem = new MenuItem("案例管理", "");
		item.addChild(subItem);
		subItem.addChild("经典案例", "rescue/case/disasterPrevent_list.jsp", target);
		
//		subItem = new MenuItem("事故救援行动管理", "");
//		item.addChild(subItem);
		return item;
	}
	
	// 系统维护
	public static MenuItem initMenuItem5(MenuItem item) {
		String target = "";
		MenuItem subItem = new MenuItem("用户管理", "infomanage/syscfg/userManage_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
//		subItem = new MenuItem("单位管理", "infomanage/syscfg/departManage_list.jsp");
//		subItem.setPosition(1);
//		item.addChild(subItem);
		
//		subItem = new MenuItem("角色管理", "infomanage/syscfg/role_list.jsp");
//		subItem.setPosition(1);
//		item.addChild(subItem);
		
		subItem = new MenuItem("单位管理", "infomanage/syscfg/department_index.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		return item;
	}
	
	// 实训演练
	public static MenuItem initMenuItem8(MenuItem item) {
		String target = "";
		MenuItem subItem = new MenuItem("质量标准化训练管理", "");
		subItem.setSelected(true);
		item.addChild(subItem);
		subItem.addChild("训练成绩统计分析", "infomanage/worklog/attendance_list.jsp", target);
		MenuItem sub2Item = new MenuItem("一般技术操作训练", "welcomePage.htm");
		subItem.addChild(sub2Item);
			    sub2Item.addChild("挂风障操作", "qs/testmgr/generaltest/guafengzhang_list.jsp", target)
			   .addChild("建造木板密封墙操作", "infomanage/worklog/atworklog_list.jsp", target)
			   .addChild("建造砖密封墙操作", "infomanage/worklog/atworklog_list.jsp", target)
			   .addChild("架木棚操作", "infomanage/worklog/atworklog_list.jsp", target)
			   .addChild("安装局部通风机和接风筒操作", "infomanage/worklog/dutyPerson_list.jsp", target)
			   .addChild("安装高倍数泡沫灭火机操作", "infomanage/worklog/statis_list.jsp", target)
			   .addChild("安装惰性气体发生装置或惰泡装置操作", "infomanage/worklog/statis_list.jsp", target)
			   .addChild("安装接水管操作", "infomanage/worklog/statis_list.jsp", target)
			   .addChild("高温浓烟训练", "infomanage/worklog/statis_list.jsp", target)
			   .addChild("仪器操作训练", "qs/testmgr/apparatustest/4h_list.jsp", target)
			   .addChild("万米配氧训练", "qs/testmgr/apparatustest/4h_list.jsp", target);
		
		subItem = new MenuItem("质量标准化考核管理", "");
		item.addChild(subItem);
		subItem.addChild("对单位考核", "infomanage/assess/assess_list.jsp", target)
			   .addChild("队员考核", "infomanage/assess/assess_person_list.jsp", target)
			   .addChild("大队考核评分统计", "train/train_list.jsp", target)
			   .addChild("中队考核评分统计", "train/train_list.jsp", target)
			   .addChild("队员考核评分统计", "train/train_list.jsp", target);
		subItem = new MenuItem("规章制度管理", "");
		item.addChild(subItem);
		subItem.addChild("对单位考核", "infomanage/assess/assess_list.jsp", target)
			   .addChild("队员考核", "infomanage/assess/assess_person_list.jsp", target)
			   .addChild("大队考核评分统计", "train/train_list.jsp", target)
			   .addChild("中队考核评分统计", "train/train_list.jsp", target)
			   .addChild("队员考核评分统计", "train/train_list.jsp", target);
		subItem = new MenuItem("各种记录管理", "");
		item.addChild(subItem);
		subItem.addChild("对单位考核", "infomanage/assess/assess_list.jsp", target)
		       .addChild("队员考核", "infomanage/assess/assess_person_list.jsp", target)
		       .addChild("大队考核评分统计", "train/train_list.jsp", target)
		       .addChild("中队考核评分统计", "train/train_list.jsp", target)
		       .addChild("队员考核评分统计", "train/train_list.jsp", target);
		subItem = new MenuItem("技术装备管理", "");
		item.addChild(subItem);
		subItem.addChild("对单位考核", "infomanage/assess/assess_list.jsp", target)
		       .addChild("队员考核", "infomanage/assess/assess_person_list.jsp", target)
		       .addChild("大队考核评分统计", "train/train_list.jsp", target)
		       .addChild("中队考核评分统计", "train/train_list.jsp", target)
		       .addChild("队员考核评分统计", "train/train_list.jsp", target);
		subItem = new MenuItem("组织机构管理", "");
		item.addChild(subItem);
		subItem.addChild("对单位考核", "infomanage/assess/assess_list.jsp", target)
		       .addChild("队员考核", "infomanage/assess/assess_person_list.jsp", target)
		       .addChild("大队考核评分统计", "train/train_list.jsp", target)
		       .addChild("中队考核评分统计", "train/train_list.jsp", target)
		       .addChild("队员考核评分统计", "train/train_list.jsp", target);
		subItem = new MenuItem("绩效考核管理", "");
		item.addChild(subItem);
		subItem.addChild("对单位考核", "infomanage/assess/assess_list.jsp", target)
		       .addChild("队员考核", "infomanage/assess/assess_person_list.jsp", target)
		       .addChild("大队考核评分统计", "train/train_list.jsp", target)
		       .addChild("中队考核评分统计", "train/train_list.jsp", target)
		       .addChild("队员考核评分统计", "train/train_list.jsp", target);		
		return item;
	}
	
//	public static MenuItem initMenuItem8(MenuItem item) {
//		String target = "";
//		MenuItem subItem = new MenuItem("对单位考核", "infomanage/assess/assess_list.jsp");
//		subItem.setPosition(1);
//		item.addChild(subItem);
//		
//		subItem = new MenuItem("对队员考核", "infomanage/assess/assess_person_list.jsp");
//		subItem.setPosition(1);
//		item.addChild(subItem);
//		
//		return item;
//	}
	// 应急通信
	public static MenuItem initMenuItem9(MenuItem item) {
		MenuItem subItem = new MenuItem("重庆松藻救护大队", "infomanage/videomonitor/ddMonitor_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		subItem = new MenuItem("打通一矿救护中队", "infomanage/videomonitor/dtykMonitor_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		
		subItem = new MenuItem("石壕煤矿救护中队", "infomanage/videomonitor/shmkMonitor_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		subItem = new MenuItem("梨园煤矿救护中队", "infomanage/videomonitor/lymkMonitor_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		subItem = new MenuItem("逢春煤矿救护中队", "infomanage/videomonitor/fcmkMonitor_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		subItem = new MenuItem("桐华煤矿救护中队", "infomanage/videomonitor/thmkMonitor_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		subItem = new MenuItem("渝阳煤矿救护中队", "infomanage/videomonitor/yymkMonitor_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		subItem = new MenuItem("松藻煤矿救护中队", "infomanage/videomonitor/szmkMonitor_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		subItem = new MenuItem("松藻煤电公司救护中队", "infomanage/videomonitor/szmdMonitor_list.jsp");
		subItem.setPosition(1);
		item.addChild(subItem);
		return item;
	}
	
	// 车辆监控
	public static MenuItem initMenuItem10(MenuItem item) {
		return item;
	}
	/**
	 * 构建右侧子菜单
	 * @return
	 */
	public static String buildSubMenu(User user, String itemIndex) {
		String html = "";
		
		if(itemIndex == null || itemIndex.equals("")) {
			return html;
		}
		
		// 默认
		if("-1".equals(itemIndex)) {
			itemIndex = "0_0";
		}
		
		String idxs[] = itemIndex.split("_");
		
		if(menu == null) {
			initMenuItems(user);
		}
		
		if(idxs.length == 0) {
			return html;
		}
		
		MenuItem item = menu.getItemByIndex(idxs[0]);
		
		if(item == null) {
			return html;
		}
		for(int i = 1; i < idxs.length; i++) {
			if(item.getChildren().length > 0) {
				item = item.getItemByIndex(idxs[i]);
			}
		}
		
		html = viewSubMenuItem(item);
		
		return html;
	}
	
	/**
	 * 设置上级菜单项的最根上的第一个子菜单为选中
	 * @param item
	 */
	private static void setItemSelected(MenuItem item) {
		if(item == null) return;
		
		boolean hasChild = item.getChildren().length > 0;
		if(!hasChild) return;
		
		while(hasChild) {
			item = item.getChildren()[0];
			hasChild = item.getChildren().length > 0;
			if(!hasChild) {
				item.setSelected(true);
			}
		}
	}
	
	/**
	 * 显示左侧子菜单
	 * @param item
	 * @return
	 */
	public static String viewSubMenuItem(MenuItem item) {
		// 设置默认选中第一个菜单
		//setItemSelected(item);
		
		StringBuffer html = new StringBuffer();
		
		if(item == null || item.getChildren().length == 0 || !item.isShow()) {
			return "";
		}
		
		//html.append("<ul>");
		for(MenuItem child : item.getChildren()) {
			viewSubMenuItem(html, child);
		}
		//html.append("</ul>");
		return html.toString();
	}
	
	/**
	 * 用于递归输出菜单html
	 * @param html
	 * @param item
	 */
	private static void viewSubMenuItem(StringBuffer html, MenuItem item){
		boolean hasChild = (item.getChildren().length > 0);
		
		//html.append("<li>");
		String indexStr = item.getIndex();
		String orderStr = indexStr.substring(indexStr.lastIndexOf("_") + 1);
		html.append("<div class='submenu_item_child' ").append("style='padding-left:15px'").append(">");
		html.append("<a ").append("href='#' ")
						  .append("id='" + indexStr + "' ")
					      .append("src='"+ item.getHref() + "' ")
						  .append("target='" + item.getTarget() + "' ")
						  .append("index='" + indexStr + "' ")
						  .append("order='" + orderStr + "' ")
						  .append("pindex='" + item.getPindex() + "' ")
						  .append("level='" + item.getLevel() + "' ")
						  .append("position='" + item.getPosition() + "' ")
						  .append("isSelected='" + item.isSelected() +"' ")
						  .append("hasChild='" + hasChild + "' ")
						  .append("type='" + item.getType() + "' ")
						  .append("onclick='clickMenuItem();' ")
						  .append(">")
			.append(item.getText())
			.append("</a>");
		
		
		if(hasChild) {
			html.append("<div class='submenu_item_parent' ").append("style='padding-left:0px'").append(">");;
			for(MenuItem child : item.getChildren()) {
				viewSubMenuItem(html, child);
			}
			html.append("</div>");
		}
		html.append("</div>");
		//html.append("</li>");
	}
	
	/**
	 * 获取菜单全标题
	 * @param itemIndex
	 * @return
	 */
	public static String getMenuTitle(String itemIndex) {
		String title = "";
		
		if(itemIndex == null || itemIndex.equals("")) {
			return title;
		}
		
		// 默认
		if("-1".equals(itemIndex)) {
			itemIndex = "0_0";
		}
		
		String idxs[] = itemIndex.split("_");
		
		MenuItem item = menu.getItemByIndex(idxs[0]);
		
		if(item == null) {
			return title;
		}
		
		title = item.getText();
		for(int i = 1; i < idxs.length; i++) {
			if(item.getChildren().length > 0) {
				item = item.getItemByIndex(idxs[i]);
				if(item == null) {
					continue;
				}
				title += "—" + item.getText();
			}
		}
		
		return title;
	}
	
	/**
	 * 获取当前菜单层级
	 * @param itemIndex
	 * @return
	 */
	public static int getCurrentLevel(String itemIndex) {
		int level = 0 ;
		if(itemIndex == null || itemIndex.equals("")) {
			return level;
		}
		
		// 默认
		if("-1".equals(itemIndex)) {
			itemIndex = "0_0";
		}
		
		String idxs[] = itemIndex.split("_");
		
		if(idxs != null) {
			level = idxs.length + 1;
		}
		
		return level;
	}
	
	public static void main(String[] args) {
		String html = buildMainMenu(null);
		System.out.println(html);
	}
}
